package vn.candyapp.qlct;


import vn.candyapp.qlct.database.DBAdapter;
import vn.candyapp.qlct.fragment.BaseFragment;
import vn.candyapp.qlct.fragment.TabCaiDat;
import vn.candyapp.qlct.fragment.TabThongKe;
import vn.candyapp.qlct.fragment.TabThuChi;
import vn.candyapp.qlct.helper.ITabSelected;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;

public class MainActivity extends BaseActivity implements TabListener {

	// duc anh commit
	private ActionBar actionBar;
	private ViewPager viewPager;
	private TabsPagerAdapter mAdapter;
	private DBAdapter dbAdapter;
	
	private String[] tabs;
	
	public void initFirstLaunch() {
		SharePreferencesManager prefMgr = SharePreferencesManager.newInstance(getApplicationContext());
		if (prefMgr.isFirstLaunch()) {
			dbAdapter.createDatabase();
			prefMgr.setFirstLaunch(false);
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		tabs = getResources().getStringArray(R.array.main_tabs_name);
		dbAdapter = new DBAdapter(getApplicationContext());
		initFirstLaunch();
		viewPager = (ViewPager) findViewById(R.id.pager);
		actionBar = getSupportActionBar();
		mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
		viewPager.setAdapter(mAdapter);
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
		for (String tabName : tabs) {
			actionBar.addTab(actionBar.newTab().setText(tabName).setTabListener(this));
		}
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				actionBar.setSelectedNavigationItem(position);
				BaseFragment f = getFragmentAtPos(position);
				if (f != null && f instanceof ITabSelected) {
					((ITabSelected) f).onRefresh();
				}
			}
			
			@Override
			public void onPageScrolled(int position, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int position) {
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onTabReselected(Tab tab, FragmentTransaction ft) {
		
	}

	@Override
	public void onTabSelected(Tab tab, FragmentTransaction ft) {
		viewPager.setCurrentItem(tab.getPosition());
	}

	@Override
	public void onTabUnselected(Tab tab, FragmentTransaction ft) {
		
	}
	
	public class TabsPagerAdapter extends FragmentPagerAdapter {
		
		public TabsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int index) {
			
			Fragment fragment = null;
			
			switch(index) {
				case 0:
					fragment = TabThuChi.newInstance();			// vn.candyapp.qlct.fragment.TabThuChi
					break;
				case 1:
					fragment = TabThongKe.newInstance();		// vn.candyapp.qlct.fragment.TabThongKe
					break;
				case 2:
					fragment = TabCaiDat.newInstance();			// vn.candyapp.qlct.fragment.TabCaiDat
					break;
			}
			
			addFragmentToStack((BaseFragment) fragment);
			return fragment;
		}

		@Override
		public int getCount() {
			return tabs.length;
		}
		
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		try { dbAdapter.close(); } catch(Exception e) {
			e.printStackTrace();
		}
	}
	
	
}
